package com.atguigu.educms.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.atguigu.commonutils.R;
import com.atguigu.educms.entity.CrmBanner;
import com.atguigu.educms.mapper.CrmBannerMapper;
import com.atguigu.educms.service.CrmBannerService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 首页banner表 服务实现类
 * </p>
 *
 * @author ZhanK
 * @since 2022-05-10
 */
@Service
public class CrmBannerServiceImpl extends ServiceImpl<CrmBannerMapper, CrmBanner> implements CrmBannerService {
   //这个注解开启redis缓存没有作用，因为返回的不是集合
    @Cacheable(value = "banner002",key = "'selectIndexList002'")
    @Override
    //分页查询轮播图
    public R getPageBanners(long current, long limit, CrmBanner crmBanner) {

        if (ObjectUtil.isNull(crmBanner)) {
            Page<CrmBanner> crmBannerPage = new Page<>(current, limit);
            QueryWrapper<CrmBanner> wrapper = new QueryWrapper<>();
            wrapper.orderByDesc("gmt_modified");
            baseMapper.selectPage(crmBannerPage, wrapper);
            long total = crmBannerPage.getTotal();
            List<CrmBanner> records = crmBannerPage.getRecords();
            HashMap<String, Object> map = new HashMap<>();
            map.put("total", total);
            map.put("rows", records);
            return R.ok().data(map);
            //return R.ok().data("total",total).data("rows", records);

        } else {

            QueryWrapper<CrmBanner> wrapper = new QueryWrapper<>();
            if (!ObjectUtil.isEmpty(crmBanner.getTitle())) {
                wrapper.like("title", crmBanner.getTitle());
            }
            if (!StringUtils.isEmpty(crmBanner.getGmtModified())) {
                wrapper.ge("gmt_modified", crmBanner.getGmtCreate());
            }
            if (!StringUtils.isEmpty(crmBanner.getGmtModified())) {
                wrapper.le("gmt_modified", crmBanner.getGmtModified());
            }
            Page<CrmBanner> crmBannerPage = new Page<>(current, limit);
            wrapper.orderByDesc("gmt_modified");
            baseMapper.selectPage(crmBannerPage, wrapper);
            long total = crmBannerPage.getTotal();
            List<CrmBanner> records = crmBannerPage.getRecords();
//            HashMap<String, Object> map = new HashMap<>();
//            map.put("total", total);
//            map.put("rows", records);
////            return R.ok().data(map);
            return R.ok().data("total", total).data("rows", records);
        }

    }


    @Override
    public List<CrmBanner> selectAllBanner() {
        List<CrmBanner> list = this.list(new LambdaQueryWrapper<CrmBanner>().orderByDesc(CrmBanner::getSort));
        return list;
    }
}
